package com.agileai.pm.controller;

import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.bizmoduler.frame.OnlineCounterService;
import com.agileai.hotweb.common.BeanFactory;
import com.agileai.hotweb.common.Constants;
import com.agileai.hotweb.common.Constants.FrameHandlers;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProjectInfoManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.util.StringUtil;

public class NavigaterHandler extends BaseHandler{
	public static final String  LOGIN_USER = "loginUser";
	
	public NavigaterHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
	    
		setAttribute(LOGIN_USER, getUser());
		String onlineAccount = this.dispatchServlet.getInitParameter(Constants.OnlineCounterServiceKey);
		if (!StringUtil.isNullOrEmpty(onlineAccount)){
			OnlineCounterService onlineCounterService = (OnlineCounterService)BeanFactory.instance().getBean(onlineAccount);
			int onlineCount = onlineCounterService.queryOnlineCount();
			setAttribute("onlineCount", onlineCount);
		}
		
		User user = (User)this.getUser();
		String userId = user.getUserId(); 
		ProjectInfoManage projectInfoManage = (ProjectInfoManage) this.lookupService(ProjectInfoManage.class);
		List<Project> projectList = projectInfoManage.queryProjectList(userId);
		
		String projectId = param.stringValue("projectSelect");
		FormSelect formSelect = new FormSelect();
		
		Project currentProject = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		
		String selectedProjectId = null; 
		if(projectList.size()>0){
			for (int i=0;i < projectList.size();i++){
				Project temp = projectList.get(i);
				formSelect.addValue(temp.getProjectId(), temp.getProjectName());
			}
			
			if(StringUtil.isNullOrEmpty(projectId)){
				String sessionProjectId = currentProject.getProjectId();
				if (StringUtil.isNotNullNotEmpty(sessionProjectId)){
					formSelect.setSelectedValue(sessionProjectId);
				}else{
					Project firstProject = projectList.get(0);
					projectId = firstProject.getProjectId();
					formSelect.setSelectedValue(projectId);
				}
			}else {
				formSelect.setSelectedValue(projectId);
			}
			selectedProjectId = formSelect.getSelectedValue();
		}
		this.setAttribute("projectSelect", formSelect);
		
		currentProject = this.getCurrentProject(projectList,selectedProjectId);
		
		this.getSessionAttributes().put(Consts.ProjectSessionKey, currentProject);
		
		return new LocalRenderer(getPage());
	}
	
	private Project getCurrentProject(List<Project> projectList,String selectedProjectId){
		Project currentProject = new Project();
		for(int i = 0;i < projectList.size();i++){
			Project temp = projectList.get(i);
			if(temp.getProjectId().equals(selectedProjectId)){
				currentProject = temp;
				break;
			}
		}
		return currentProject;
	}
	
	@PageAction
	public ViewRenderer refreshSelect(DataParam param){
		setAttribute(LOGIN_USER, getUser());
		
		User user = (User)this.getUser();
		String userId = user.getUserId(); 
		ProjectInfoManage projectInfoManage = (ProjectInfoManage) this.lookupService(ProjectInfoManage.class);
		List<Project> projectList = projectInfoManage.queryProjectList(userId);
		
		String projectId = param.stringValue("projectSelect");
		FormSelect formSelect = new FormSelect();
		
		Project currentProject = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		
		for (int i=0;i < projectList.size();i++){
			Project temp = projectList.get(i);
			formSelect.addValue(temp.getProjectId(), temp.getProjectName());
		}
		
		formSelect.setSelectedValue(projectId);
			
		this.setAttribute("projectSelect", formSelect);
		
		currentProject = this.getCurrentProject(projectList,projectId);
		
		this.getSessionAttributes().put(Consts.ProjectSessionKey, currentProject);
		return new AjaxRenderer(formSelect.toString());
	}
	
	
	public ViewRenderer doLogoutAction(DataParam param){
		String casServerLogoutUrl = this.dispatchServlet.getServletContext().getInitParameter("casServerLogoutUrl");
		if (!StringUtil.isNullOrEmpty(casServerLogoutUrl)){
			String serverName = this.dispatchServlet.getServletContext().getInitParameter("serverName");
			String redirectUrl = casServerLogoutUrl + "?service="+ serverName+this.dispatchServlet.getServletContext().getContextPath();
			return new RedirectRenderer(redirectUrl);
		}else{
			return new RedirectRenderer(getHandlerURL(FrameHandlers.LoginHandlerId)+"&actionType=logout");			
		}
	}
}
